﻿using Prism.Regions;
using System;
using Telerik.Windows.Controls;

namespace SmartPlantApp.Common;

public class DockingRegionAdapter : RegionAdapterBase<RadDocking>
{
    public DockingRegionAdapter(IRegionBehaviorFactory regionBehaviorFactory)
        : base(regionBehaviorFactory)
    {
    }

    protected override void Adapt(IRegion region, RadDocking regionTarget)
    {
        if (regionTarget == null) throw new ArgumentNullException("regionTarget");
        regionTarget.PanesSource = region.Views;

        //bool itemsSourceIsSet = regionTarget.ItemsSource != null;

        //if (itemsSourceIsSet)
        //{
        //    throw new InvalidOperationException("ItemsControlHasItemsSourceException");
        //}
    }

    protected override IRegion CreateRegion()
    {
        return new Region();
    }

    /// <summary> 
    /// 附加新行为。
    /// </summary> 
    /// <param name="region">使用的区域。</param> 
    /// <param name="regionTarget">要调整的对象。</param> 
    /// <remarks> 
    /// 此类附加基本行为，并保持<see cref=“RadDocking.PanesSource”/>和<see cref=“IRegion.ActiveViews”/>同步。
    /// </remarks> 
    protected override void AttachBehaviors(IRegion region, RadDocking regionTarget)
    {
        if (region == null) throw new ArgumentNullException("region");
        base.AttachBehaviors(region, regionTarget);
        if (!region.Behaviors.ContainsKey(DockingActivePaneSyncBehavior.BehaviorKey))
        {
            var docking = new DockingActivePaneSyncBehavior { HostControl = regionTarget };
            region.Behaviors.Add(DockingActivePaneSyncBehavior.BehaviorKey, docking);
        }
    }
}
